package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 套餐相关接口业务层
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper ;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //初始化分页查询参数
        PageHelper.startPage(setmealPageQueryDTO.getPage() , setmealPageQueryDTO.getPageSize()) ;
        //执行分页查询
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO) ;
        //封装查询结果
        Long total = page.getTotal() ;
        List<SetmealVO> records = page.getResult() ;

        PageResult pageResult = new PageResult(total , records) ;
        return pageResult ;
    }

    /**
     * 套餐分页查询
     *
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        //添加一条套餐数据
        Setmeal setmeal = new Setmeal() ;
        BeanUtils.copyProperties(setmealDTO , setmeal);
        setmealMapper.insert(setmeal) ; //这里要主键返回

        //添加 n 条菜品套餐数据
        Long setmealId = setmeal.getId() ;  //获取套餐 id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();    //获取套餐下的菜品数据
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);    //为菜品加上关联的 id
        });

        setmealDishMapper.insertBatch(setmealDishes) ;

    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐数据和分类数据
        SetmealVO setmealVO = setmealMapper.getWithCategoryById(id) ;
        //查询套餐菜品数据
        List<SetmealDish> dishList = setmealDishMapper.getBySetmealId(id);
        //构造返回结果
        setmealVO.setSetmealDishes(dishList);
        return setmealVO ;
    }

    /**
     * 修改套餐和对应的菜品信息
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateWithDishes(SetmealDTO setmealDTO) {
        //先删除套餐下的所有菜品信息
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //添加套餐信息
        Setmeal setmeal = new Setmeal() ;
        BeanUtils.copyProperties(setmealDTO , setmeal);
        setmealMapper.update(setmeal) ;     //主意主键返回

        //将新的菜品信息添加回去
        Long setmealId = setmeal.getId();   //获取套餐主键
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//获取菜品
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //批量添加菜品
        setmealDishMapper.insertBatch(setmealDishes) ;

    }

    /**
     * 起售或停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //如果是起售操作的话 先判断是否存在未起售的菜品 有的话就不能起售
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id) ;
            dishList.forEach(dish -> {
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED) ;
                }
            });
        }

        //修改套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal) ;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //如果集合中有套餐位于起售状态 则不能删除
        List<Setmeal> setmealList = setmealMapper.getByIds(ids) ;
        setmealList.forEach(setmeal -> {
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //删除套餐
        setmealMapper.deleteBatch(ids) ;
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {
        Setmeal setmeal = Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build() ;
        //根据分类 id 查询套餐 这里面应该是要查询正在起售中的
        List<Setmeal> setmealList = setmealMapper.getByCategoryId(setmeal) ;
        return setmealList ;
    }

    /**
     * 根据套餐id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishesBySetmealId(Long id) {
        //套餐起售了 下面的菜品其实也必然全部起售了 这里可以不用判断菜品是否起售
        List<DishItemVO> dishItemVOList = setmealMapper.getDishesBySetmealId(id) ;
        return dishItemVOList ;
    }
}
